<?php
	/* -------------------------------------------------------------------
	IOU - expense sharing system 
	Copyright (C) 2009 - Mischa Spelt (m.spelt@gmail.com)
	
	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
	
	------------------------------------------------------------------- */
	$accounts = array();
	function printAccountNumber($memberID) {
		global $accounts, $db;
		if(!isset($accounts[$memberID]))
			$accounts[$memberID] = $db->q("value SELECT Account FROM Users WHERE ID = %i", $memberID);
		return $accounts[$memberID] ? " (" . $accounts[$memberID] . ")" : "";
	}

	function printGroupInfo($groupID) {
		global $db, $gUser;
		$group = $db->q("tuple SELECT ID, DisplayName FROM Groups WHERE ID = %i", $groupID);
		$members = array();
		foreach($db->q("table SELECT UserID, Login, DisplayName FROM Users_Groups LEFT JOIN Users ON Users_Groups.UserID = Users.ID WHERE GroupID = %i", $group['ID']) as $m)
			$members[$m['UserID']] = $m['DisplayName'] . " (" . $m['Login'] . ")";
	
		print "<h1>Group overview: {$group['DisplayName']}</h1>";
		print "<table class='center'><tr><th></th><th style='width: 5em; text-align: left;'>Debt</th><th style='width: 5em; text-align: left;'>Credit</th></tr>";
		foreach($members as $member=>$display) {
			$balances[$member] = $db->q("value SELECT SUM(Amount / Divisor) FROM Journal LEFT JOIN Bills ON Journal.BillID = Bills.ID WHERE Bills.GroupID = %i AND Journal.UserID = %i", $group['ID'], $member);
			$isActive = $db->q("value SELECT IsActive FROM Users_Groups WHERE UserID = %i AND GroupID = %i", $member, $group['ID']);
			if ($isActive || abs($balances[$member]) > 0.01)
			{
				print "<tr><td>$display</td><td>" . ($balances[$member] > 0 ? money_format(moneyFormat, $balances[$member]) : "") . "</td><td>" . ($balances[$member] < 0 ? money_format(moneyFormat, -$balances[$member]) : "") . "</td></tr>";
			}
		}
		print "</table>";


		if(array_filter($balances)):
		print "<h1 style='background-color: #d3d3d3;'>Proposed settlement</h1><table class='center'><tr><td>";
                define('EPSILON', 0.011);
		if(abs(array_sum($balances)) > EPSILON)
			print "<p>The system did not try to find the most efficient settlement, because the journal seems to be unbalanced.</p>";
		else {
			print "<table style='width: 100%;'>";
			do {
				// First filter 0 elements and sort the array
				$balances = array_filter($balances);
				asort($balances);
				// Find the highest and lowest amounts
				$keys = array_keys($balances);
				$idxLow = $keys[0];
				$idxHigh = $keys[count($balances) - 1];		
				// Assign the highest debt to the highest credit
				$amount = min(-$balances[$idxLow], $balances[$idxHigh]);
				if(abs($amount) < 1E-2) break;
				$class = "";
				if($idxLow == $gUser['ID']) $class = " class='paytome'"; // idxLow = receiving user
				if($idxHigh == $gUser['ID']) $class = " class='paybyme'"; // idxHigh = paying user
				print "<tr$class><td>" . $members[$idxHigh] . "</td><td>pays " . money_format(moneyFormat, $amount) . " </td><td>to " . $members[$idxLow] . ($idxHigh == $gUser['ID'] ? printAccountNumber($idxLow) : "") .  "</td></tr>";
				$balances[$idxLow] += $amount;
				$balances[$idxHigh] -= $amount;		
			} while(1);
			print "</table>";	
		}
		print "</td></tr></table>";
		endif;
	}
?>
